<template>
	<div>
		<div class="container">
			<div class="handle-box">
				<el-input v-model="query.name" placeholder="请输入分类名称" class="width260 margin-right20"></el-input>
				<el-button type="primary" @click="handleSearch">查询</el-button>
				<el-button @click="handleRefresh">重置</el-button>
				<el-button type="primary" @click="handleEdit">新建
				</el-button>
			</div>
			<el-table :data="tableData" border class="margin-top20" header-cell-class-name="table-header">
				<el-table-column type="index" width="55" align="center" label="序号"></el-table-column>
				<el-table-column prop="name" label="分类名称" align="center"></el-table-column>
				<el-table-column prop="sort" label="排序" align="center"></el-table-column>
				<el-table-column label="操作" width="180" align="center">
					<template slot-scope="scope">
						<el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑
						</el-button>
						<el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="margin-top10">
				<el-pagination background layout="->,total, prev, pager, next" :current-page="query.pageNumber"
					:page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
			</div>
		</div>

		<!-- 编辑弹出框 -->
		<el-dialog append-to-body :title="dialogTitle" :visible.sync="editVisible" width="30%" class="tankuangIpt">
			<el-form ref="form" :model="form" label-width="70px">
				<el-form-item label="分类名称" :required="true" label-width="78px">
					<el-input v-model="form.name"></el-input>
				</el-form-item>
				<el-form-item label="排序" label-width="78px">
					<el-input v-model="form.sort"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="editVisible = false">取 消</el-button>
				<el-button type="primary" @click="addQuestionSort()">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		props: ['type'],
		name: 'categoryTable',
		data() {
			return {
				query: {
					name: '',
					type: '',
					pageNumber: 1,
					pageSize: 30
				},
				dialogTitle: '',
				tableData: [],
				editVisible: false,
				pageTotal: 0,
				form: {},
			};
		},
		created() {
			this.getData();
		},
		methods: {
			async getData() {
				this.query.type = this.type
				const res = await this.$get('/admin/questionVideo/getVideoSort', this.query)
				if (res.code == 1000) {
					this.tableData = res.data.list
					this.pageTotal = res.data.totalRow
				}
			},
			// 触发搜索按钮
			handleSearch() {
				this.query.pageNumber = 1
				this.getData();
			},
			handleRefresh() {
				this.query = {
					pageNumber: 1,
					pageSize: 30
				}
				this.getData();
			},
			// 删除操作
			handleDelete(index, row) {
				this.$confirm('确定要删除吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.$post('/admin/questionVideo/deleteVideoCategory', {
						id: row.id
					}).then(() => {
						this.$message.success('删除成功');
						this.tableData.splice(index, 1);
					}).catch(() => {});
				})
			},
			async addQuestionSort() {
				if (this.$.isEmpty(this.form.name)) {
					this.$message.warning('请输入分类名称!')
					return
				}
				this.form.type = this.type
				const res = await this.$post('/admin/questionVideo/saveCategory', this.form)
				if (res.code == 1000) {
					this.$message.success('操作成功')
					this.getData()
					this.editVisible = false
				}
			},
			// 编辑操作
			handleEdit(row) {
				if (row.id) {
					this.dialogTitle = '编辑'
					this.form = JSON.parse(JSON.stringify(row))
				} else {
					this.dialogTitle = "新增"
					this.form = {}
				}
				this.editVisible = true;
			},
			// 分页导航
			handlePageChange(val) {
				this.query.pageNumber = val
				this.getData();
			}
		}
	};
</script>

<style scoped>
</style>